Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts ---------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

#WRF C LOD values are coming up as NA, should be 0? 

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
        p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Combine the two main plot pieces as a subplot

p_combined <-
    plotly::subplot(p2,p1, # plots to combine, top to bottom
      nrows = 2,
      heights = c(.6,.4),  # relative heights of the two plots
      shareX = TRUE,  # plots will share an X axis
      titleY = TRUE
    ) %>%
    # create a vertical "spike line" to compare data across 2 plots
    plotly::layout(
      xaxis = list(
        spikethickness = 1,
        spikedash = "dot",
        spikecolor = "black",
        spikemode = "across+marker",
        spikesnap = "cursor"
      ),
      yaxis = list(spikethickness = 0)
    )
## Warning: Ignoring 1 observations
p_combined

Save the plot to pull into the index

#save(p_combined, file = "./plotly_fig.rda")

Save an htmlwidget for website embedding

#htmlwidgets::saveWidget(p_combined, "plotly_fig.html")
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#build a function here to make smooth frames so we don't repeat everything in huge loops
#FOR INDIVIDUAL FIGURES ONLY
make_n1_smooth_frame <- function(df){
  smooth_n1 <- df %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N1")
  return(smooth_n1)
}

make_n2_smooth_frame <- function(df){
  smooth_n1 <- df %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N2")
  return(smooth_n1)
}

#run frames through the functions
wrfa_smooth_n1 <- make_n1_smooth_frame(wrf_a_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfb_smooth_n1 <- make_n1_smooth_frame(wrf_b_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfc_smooth_n1 <- make_n1_smooth_frame(wrf_c_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfa_smooth_n2 <- make_n2_smooth_frame(wrf_a_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfb_smooth_n2 <- make_n2_smooth_frame(wrf_b_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfc_smooth_n2 <- make_n2_smooth_frame(wrf_c_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#get max date
maxdate <- max(wrfa_smooth_n1$date)
mindate <- min(wrfa_smooth_n1$date)

Build loess smoothing figures figures

#COMBINED FIGURE ONLY
#create smoothing data frames 
#n1
smooth_n1 <- only_n1 %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N1")
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#n2
smooth_n2 <- only_n2 %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N2")
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#**************************************COMBINED PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1 <- ggplot(smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 127)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2 <- ggplot(smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 127)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1
## `geom_smooth()` using formula 'y ~ x'

fit_n1
##   [1] 10.90670 11.09491 11.27695 11.45247 11.62115 11.78263 11.93660 12.08270
##   [9] 12.22073 12.35103 12.47405 12.59025 12.70010 12.80405 12.90257 12.99416
##  [17] 13.07729 13.15236 13.21975 13.27983 13.33302 13.37968 13.42021 13.45499
##  [25] 13.48441 13.50887 13.52874 13.54441 13.55628 13.55454 13.53127 13.48968
##  [33] 13.43296 13.36433 13.28699 13.20413 13.11897 13.03469 12.95452 12.88164
##  [41] 12.81926 12.77059 12.73883 12.71176 12.67772 12.64073 12.60484 12.57407
##  [49] 12.55245 12.54400 12.54795 12.56028 12.58022 12.60698 12.63976 12.67780
##  [57] 12.72029 12.76646 12.81552 12.86669 12.91917 12.97219 13.02495 13.07668
##  [65] 13.13407 13.20291 13.28103 13.36623 13.45635 13.54921 13.64263 13.73444
##  [73] 13.82245 13.90449 13.97839 14.04195 14.09302 14.12941 14.15672 14.18162
##  [81] 14.20349 14.22170 14.23565 14.24472 14.24830 14.24239 14.22448 14.19618
##  [89] 14.15908 14.11480 14.06492 14.01106 13.95481 13.89778 13.84156 13.78777
##  [97] 13.73800 13.69385 13.65693 13.62264 13.58569 13.54666 13.50613 13.46467
## [105] 13.42284 13.38124 13.34043 13.30098 13.26348 13.22849 13.19660 13.16837
## [113] 13.14438 13.12334 13.10360 13.08522 13.06823 13.05269 13.03864 13.02612
## [121] 13.01520 13.00590 12.99828 12.99238 12.98825 12.98593 12.98548
#n2
extract_n2
## `geom_smooth()` using formula 'y ~ x'

fit_n2
##   [1] 10.71314 10.93445 11.14886 11.35612 11.55601 11.74830 11.93274 12.10912
##   [9] 12.27724 12.43730 12.58964 12.73457 12.87244 13.00357 13.12828 13.24573
##  [17] 13.35496 13.45621 13.54969 13.63561 13.71420 13.78566 13.85022 13.90810
##  [25] 13.95950 14.00465 14.04377 14.07707 14.10476 14.11805 14.10992 14.08327
##  [33] 14.04097 13.98593 13.92104 13.84919 13.77327 13.69617 13.62078 13.55000
##  [41] 13.48672 13.43383 13.39422 13.35296 13.29748 13.23420 13.16954 13.10992
##  [49] 13.06175 13.03145 13.01484 13.00329 12.99659 12.99452 12.99685 13.00335
##  [57] 13.01382 13.02803 13.04574 13.06675 13.09084 13.11777 13.14733 13.17929
##  [65] 13.22017 13.27499 13.34127 13.41655 13.49834 13.58419 13.67161 13.75814
##  [73] 13.84131 13.91864 13.98767 14.04593 14.09093 14.12022 14.13705 14.14723
##  [81] 14.15211 14.15303 14.15136 14.14843 14.14559 14.13874 14.12364 14.10144
##  [89] 14.07332 14.04043 14.00394 13.96500 13.92479 13.88445 13.84515 13.80805
##  [97] 13.77432 13.74512 13.72160 13.70031 13.67726 13.65279 13.62726 13.60104
## [105] 13.57447 13.54792 13.52174 13.49629 13.47193 13.44900 13.42788 13.40891
## [113] 13.39245 13.37775 13.36382 13.35069 13.33836 13.32685 13.31617 13.30634
## [121] 13.29738 13.28929 13.28210 13.27582 13.27046 13.26604 13.26257
#assign fits to a vector
n1_trend <- fit_n1
n2_trend <- fit_n2

#extract y min and max for each
limits_n1 <- ggplot_build(extract_n1)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1 <- as.data.frame(limits_n1)
n1_ymin <- limits_n1$ymin
n1_ymax <- limits_n1$ymax

limits_n2 <- ggplot_build(extract_n2)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2 <- as.data.frame(limits_n2)
n2_ymin <- limits_n2$ymin
n2_ymax <- limits_n2$ymax

#reassign dataframes (just to be safe)
work_n1 <- smooth_n1
work_n2 <- smooth_n2

#fill in missing dates to smooth fits
work_n1 <- work_n1 %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1 <- work_n1$date
work_n2 <- work_n2 %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2 <- work_n2$date

#create a new smooth dataframe to layer
smooth_frame_n1 <- data.frame(date_vec_n1, n1_trend, n1_ymin, n1_ymax)
smooth_frame_n2 <- data.frame(date_vec_n2, n2_trend, n2_ymin, n2_ymax)
#make plotlys
#**************************************COMBINED PLOT**********************************************
#plot smooth frames
p3 <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1, y = ~n1_trend,
                    data = smooth_frame_n1,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1,
                                  '</br> Median Log Copies: ', round(n1_trend, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_lines(x = ~date_vec_n2, y = ~n2_trend,
                  data = smooth_frame_n2,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2,
                                  '</br> Median Log Copies: ', round(n2_trend, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1, ymin = ~n1_ymin, ymax = ~n1_ymax,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymax, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_ymin, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2, ymin = ~n2_ymin, ymax = ~n2_ymax,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymax, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_ymin, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p3

Create final trend plot by stacking with epidemic curve

smooth_extracttest <-
    plotly::subplot(p3,p1, # plots to combine, top to bottom
      nrows = 2,
      heights = c(.6,.4),  # relative heights of the two plots
      shareX = TRUE,  # plots will share an X axis
      titleY = TRUE
    ) %>%
    # create a vertical "spike line" to compare data across 2 plots
    plotly::layout(
      xaxis = list(
        spikethickness = 1,
        spikedash = "dot",
        spikecolor = "black",
        spikemode = "across+marker",
        spikesnap = "cursor"
      ),
      yaxis = list(spikethickness = 0)
    )
## Warning: Ignoring 1 observations
smooth_extracttest
#save(smooth_extracttest, file = "./smooth_extracttest.rda")

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1a <- ggplot(wrfa_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1a<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 127)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2a <- ggplot(wrfa_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2a<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 127)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1a
## `geom_smooth()` using formula 'y ~ x'

fit_n1a
##   [1] 11.04339 11.13485 11.22333 11.30843 11.38976 11.46691 11.53949 11.60708
##   [9] 11.66952 11.72724 11.78079 11.83070 11.87752 11.92178 11.96403 12.00243
##  [17] 12.03509 12.06248 12.08509 12.10340 12.11788 12.12901 12.13728 12.14315
##  [25] 12.14711 12.14964 12.15122 12.15232 12.15342 12.14799 12.13054 12.10318
##  [33] 12.06798 12.02702 11.98240 11.93618 11.89047 11.84733 11.80886 11.77714
##  [41] 11.75425 11.74228 11.74331 11.75189 11.76169 11.77352 11.78817 11.80646
##  [49] 11.82918 11.85712 11.89195 11.93423 11.98314 12.03784 12.09751 12.16133
##  [57] 12.22847 12.29809 12.36938 12.44151 12.51365 12.58497 12.65465 12.72186
##  [65] 12.79613 12.88556 12.98719 13.09807 13.21523 13.33571 13.45655 13.57479
##  [73] 13.68747 13.79163 13.88430 13.96253 14.02336 14.06382 14.09822 14.13902
##  [81] 14.18054 14.21706 14.24287 14.25229 14.23961 14.19805 14.12768 14.03303
##  [89] 13.91865 13.78907 13.64884 13.50250 13.35458 13.20963 13.07218 12.94678
##  [97] 12.83797 12.75028 12.68826 12.63943 12.58892 12.53771 12.48677 12.43707
## [105] 12.38959 12.34529 12.30515 12.27015 12.24125 12.21943 12.20565 12.20090
## [113] 12.20614 12.21934 12.23791 12.26192 12.29146 12.32662 12.36749 12.41417
## [121] 12.46673 12.52528 12.58989 12.66065 12.73767 12.82101 12.91079
#n2
extract_n2a
## `geom_smooth()` using formula 'y ~ x'

fit_n2a
##   [1] 10.53138 10.75011 10.96206 11.16690 11.36434 11.55407 11.73577 11.90913
##   [9] 12.07396 12.23051 12.37924 12.52055 12.65487 12.78264 12.90428 13.01870
##  [17] 13.12470 13.22256 13.31257 13.39498 13.47009 13.53816 13.59947 13.65430
##  [25] 13.70291 13.74559 13.78262 13.81426 13.84080 13.85348 13.84527 13.81897
##  [33] 13.77740 13.72339 13.65974 13.58927 13.51481 13.43916 13.36515 13.29559
##  [41] 13.23329 13.18108 13.14177 13.10186 13.04973 12.99104 12.93143 12.87656
##  [49] 12.83208 12.80365 12.78768 12.77666 12.77036 12.76856 12.77102 12.77752
##  [57] 12.78782 12.80170 12.81892 12.83926 12.86248 12.88836 12.91666 12.94716
##  [65] 12.98784 13.04487 13.11527 13.19607 13.28430 13.37697 13.47112 13.56377
##  [73] 13.65194 13.73267 13.80298 13.85989 13.90044 13.92164 13.93663 13.95737
##  [81] 13.97909 13.99707 14.00655 14.00278 13.98102 13.93507 13.86457 13.77353
##  [89] 13.66595 13.54585 13.41722 13.28408 13.15044 13.02029 12.89766 12.78653
##  [97] 12.69093 12.61486 12.56232 12.52220 12.48128 12.44044 12.40056 12.36250
## [105] 12.32715 12.29537 12.26803 12.24603 12.23022 12.22148 12.22068 12.22870
## [113] 12.24642 12.27202 12.30316 12.33991 12.38235 12.43056 12.48460 12.54455
## [121] 12.61049 12.68249 12.76063 12.84497 12.93559 13.03257 13.13598
#assign fits to a vector
n1_trenda <- fit_n1a
n2_trenda <- fit_n2a

#extract y min and max for each
limits_n1a <- ggplot_build(extract_n1a)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1a <- as.data.frame(limits_n1a)
n1_ymina <- limits_n1a$ymin
n1_ymaxa <- limits_n1a$ymax

limits_n2a <- ggplot_build(extract_n2a)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2a <- as.data.frame(limits_n2a)
n2_ymina <- limits_n2a$ymin
n2_ymaxa <- limits_n2a$ymax

#reassign dataframes (just to be safe)
work_n1a <- wrfa_smooth_n1
work_n2a<- wrfa_smooth_n1

#fill in missing dates to smooth fits
work_n1a <- work_n1a %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1a <- work_n1a$date
work_n2a <- work_n2a %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2a <- work_n2a$date

#create a new smooth dataframe to layer
smooth_frame_n1a <- data.frame(date_vec_n1a, n1_trenda, n1_ymina, n1_ymaxa)
smooth_frame_n2a <- data.frame(date_vec_n2a, n2_trenda, n2_ymina, n2_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1a, y = ~n1_trenda,
                    data = smooth_frame_n1a,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1a,
                                  '</br> Median Log Copies: ', round(n1_trenda, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2a, y = ~n2_trenda,
                  data = smooth_frame_n2a,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2a,
                                  '</br> Median Log Copies: ', round(n2_trenda, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1a, ymin = ~n1_ymina, ymax = ~n1_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1a, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_ymina, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2a, ymin = ~n2_ymina, ymax = ~n2_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2a, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_ymina, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfa_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfa_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1b <- ggplot(wrfb_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1b<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 127)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2b <- ggplot(wrfb_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2b<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 127)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1b
## `geom_smooth()` using formula 'y ~ x'

fit_n1b
##   [1] 10.54475 10.70333 10.85618 11.00309 11.14382 11.27816 11.40588 11.52676
##   [9] 11.64063 11.74769 11.84829 11.94277 12.03146 12.11470 12.19283 12.26493
##  [17] 12.33001 12.38828 12.43997 12.48529 12.52448 12.55775 12.58533 12.60743
##  [25] 12.62428 12.63611 12.64312 12.64555 12.64362 12.62780 12.59059 12.53522
##  [33] 12.46494 12.38299 12.29260 12.19703 12.09951 12.00328 11.91158 11.82767
##  [41] 11.75476 11.69612 11.65497 11.61777 11.57216 11.52312 11.47569 11.43487
##  [49] 11.40566 11.39308 11.39394 11.40147 11.41532 11.43512 11.46049 11.49108
##  [57] 11.52652 11.56645 11.61050 11.65831 11.70951 11.76373 11.82062 11.87980
##  [65] 11.94929 12.03540 12.13523 12.24588 12.36443 12.48800 12.61367 12.73853
##  [73] 12.85970 12.97426 13.07931 13.17194 13.24926 13.30836 13.35300 13.38984
##  [81] 13.42035 13.44599 13.46822 13.48851 13.50830 13.52355 13.52979 13.52784
##  [89] 13.51855 13.50275 13.48127 13.45496 13.42465 13.39117 13.35536 13.31806
##  [97] 13.28010 13.24231 13.20554 13.16700 13.12381 13.07667 13.02626 12.97329
## [105] 12.91844 12.86241 12.80589 12.74958 12.69416 12.64034 12.58880 12.54025
## [113] 12.49536 12.45251 12.40963 12.36673 12.32386 12.28104 12.23831 12.19570
## [121] 12.15324 12.11097 12.06891 12.02710 11.98558 11.94436 11.90349
#n2
extract_n2b
## `geom_smooth()` using formula 'y ~ x'

fit_n2b
##   [1] 10.32424 10.49313 10.65633 10.81407 10.96658 11.11409 11.25683 11.39503
##   [9] 11.52873 11.65764 11.78150 11.90007 12.01307 12.12026 12.22138 12.31696
##  [17] 12.40760 12.49316 12.57350 12.64848 12.71795 12.78178 12.83982 12.89193
##  [25] 12.93796 12.97778 13.01125 13.03821 13.05853 13.06762 13.06220 13.04406
##  [33] 13.01500 12.97682 12.93131 12.88029 12.82554 12.76887 12.71206 12.65694
##  [41] 12.60528 12.55889 12.51958 12.46124 12.36655 12.25038 12.12764 12.01323
##  [49] 11.92204 11.86896 11.84129 11.81669 11.79562 11.77852 11.76585 11.75806
##  [57] 11.75559 11.75891 11.76846 11.78470 11.80808 11.83904 11.87805 11.92555
##  [65] 11.99049 12.07892 12.18691 12.31052 12.44583 12.58890 12.73580 12.88262
##  [73] 13.02541 13.16025 13.28321 13.39035 13.47776 13.54150 13.59084 13.63702
##  [81] 13.67905 13.71594 13.74671 13.77035 13.78589 13.78952 13.77912 13.75630
##  [89] 13.72263 13.67970 13.62909 13.57239 13.51119 13.44706 13.38160 13.31638
##  [97] 13.25300 13.19303 13.13807 13.08310 13.02266 12.95764 12.88891 12.81737
## [105] 12.74390 12.66937 12.59469 12.52073 12.44838 12.37852 12.31204 12.24982
## [113] 12.19274 12.13880 12.08543 12.03267 11.98059 11.92925 11.87869 11.82898
## [121] 11.78017 11.73232 11.68549 11.63973 11.59509 11.55164 11.50943
#assign fits to a vector
n1_trendb <- fit_n1b
n2_trendb <- fit_n2b

#extract y min and max for each
limits_n1b <- ggplot_build(extract_n1b)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1b <- as.data.frame(limits_n1b)
n1_yminb <- limits_n1b$ymin
n1_ymaxb <- limits_n1b$ymax

limits_n2b <- ggplot_build(extract_n2b)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2b <- as.data.frame(limits_n2b)
n2_yminb <- limits_n2b$ymin
n2_ymaxb <- limits_n2b$ymax

#reassign dataframes (just to be safe)
work_n1b <- wrfb_smooth_n1
work_n2b<- wrfb_smooth_n1

#fill in missing dates to smooth fits
work_n1b <- work_n1b %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1b <- work_n1b$date
work_n2b <- work_n2b %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2b <- work_n2b$date

#create a new smooth dataframe to layer
smooth_frame_n1b <- data.frame(date_vec_n1b, n1_trendb, n1_yminb, n1_ymaxb)
smooth_frame_n2b <- data.frame(date_vec_n2b, n2_trendb, n2_yminb, n2_ymaxb)

#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1b, y = ~n1_trendb,
                    data = smooth_frame_n1b,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1b,
                                  '</br> Median Log Copies: ', round(n1_trendb, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2b, y = ~n2_trendb,
                  data = smooth_frame_n2b,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2b,
                                  '</br> Median Log Copies: ', round(n2_trendb, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1b, ymin = ~n1_yminb, ymax = ~n1_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1b, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_yminb, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2b, ymin = ~n2_yminb, ymax = ~n2_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2b, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_yminb, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfb_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfb_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** Does not work until raw data fixed #add trendlines #extract data from geom_smooth #n1 extract # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_n1c <- ggplot(wrfc_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1c<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 106)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2c <- ggplot(wrfc_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2c<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 106)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1c
## `geom_smooth()` using formula 'y ~ x'

fit_n1c
##   [1] 12.60321 12.56439 12.52676 12.49036 12.45525 12.42146 12.38907 12.35810
##   [9] 12.32861 12.30065 12.27427 12.24952 12.22644 12.20509 12.18551 12.16879
##  [17] 12.15547 12.14480 12.13602 12.12838 12.12111 12.11348 12.10562 12.09827
##  [25] 12.09157 12.08565 12.08064 12.07668 12.07391 12.08108 12.10295 12.13364
##  [33] 12.16727 12.19799 12.21991 12.22717 12.24937 12.30989 12.39351 12.48501
##  [41] 12.56918 12.63080 12.65464 12.63233 12.57436 12.49384 12.40388 12.31760
##  [49] 12.24811 12.20853 12.16955 12.10135 12.01629 11.92668 11.84486 11.78318
##  [57] 11.75398 11.76516 11.80842 11.87170 11.94297 12.01019 12.06130 12.08427
##  [65] 12.11309 12.17889 12.26536 12.35618 12.43506 12.48568 12.49172 12.45769
##  [73] 12.40266 12.33213 12.25162 12.16665 12.08274 12.00540 11.91506 11.79631
##  [81] 11.66084 11.52038 11.38665 11.27136 11.18623 11.11587 11.03926 10.95962
##  [89] 10.88017 10.80414 10.73477 10.67527 10.62096 10.56598 10.51177 10.45975
##  [97] 10.41138 10.36809 10.33132 10.30081 10.27512 10.25386 10.23668 10.22320
## [105] 10.21306 10.20589
#n2
extract_n2c
## `geom_smooth()` using formula 'y ~ x'

fit_n2c
##   [1] 13.40786 13.29592 13.18623 13.07894 12.97421 12.87218 12.77300 12.67683
##   [9] 12.58381 12.49410 12.40784 12.32519 12.24629 12.17130 12.10037 12.03196
##  [17] 11.96510 11.90079 11.83999 11.78370 11.73291 11.68859 11.64375 11.59313
##  [25] 11.54034 11.48899 11.44266 11.40499 11.37955 11.36709 11.36468 11.37049
##  [33] 11.38268 11.39938 11.41878 11.43901 11.48134 11.56071 11.66578 11.78521
##  [41] 11.90767 12.02182 12.11632 12.21832 12.35311 12.50662 12.66473 12.81336
##  [49] 12.93842 13.02581 13.08539 13.13628 13.17822 13.21094 13.23418 13.24769
##  [57] 13.25122 13.24396 13.22585 13.19772 13.16037 13.11464 13.06134 13.00130
##  [65] 12.91371 12.78716 12.63652 12.47669 12.32255 12.18901 12.09093 12.00133
##  [73] 11.89010 11.76749 11.64374 11.52909 11.43380 11.36810 11.33890 11.33871
##  [81] 11.35620 11.38002 11.39881 11.40124 11.37596 11.33503 11.29736 11.26187
##  [89] 11.22749 11.19315 11.15777 11.12028 11.08319 11.04896 11.01644 10.98446
##  [97] 10.95188 10.91753 10.88026 10.84016 10.79824 10.75473 10.70988 10.66391
## [105] 10.61708 10.56960
#assign fits to a vector
n1_trendc <- fit_n1c
n2_trendc <- fit_n2c

#extract y min and max for each
limits_n1c <- ggplot_build(extract_n1c)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1c <- as.data.frame(limits_n1c)
n1_yminc <- limits_n1c$ymin
n1_ymaxc <- limits_n1c$ymax

limits_n2c <- ggplot_build(extract_n2c)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2c <- as.data.frame(limits_n2c)
n2_yminc <- limits_n2c$ymin
n2_ymaxc <- limits_n2c$ymax

#reassign dataframes (just to be safe)
work_n1c <- wrfc_smooth_n1
work_n2c <- wrfc_smooth_n1

#fill in missing dates to smooth fits
work_n1c <- work_n1c %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1c <- work_n1c$date
work_n2c <- work_n2c %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2c <- work_n2c$date

#create a new smooth dataframe to layer
smooth_frame_n1c <- data.frame(date_vec_n1c, n1_trendc, n1_yminc, n1_ymaxc)
smooth_frame_n2c <- data.frame(date_vec_n2c, n2_trendc, n2_yminc, n2_ymaxc)


#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1c, y = ~n1_trendc,
                    data = smooth_frame_n1c,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1c,
                                  '</br> Median Log Copies: ', round(n1_trendc, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
   layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2c, y = ~n2_trendc,
                  data = smooth_frame_n2c,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2c,
                                  '</br> Median Log Copies: ', round(n2_trendc, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1c, ymin = ~n1_yminc, ymax = ~n1_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1c, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_yminc, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2c, ymin = ~n2_yminc, ymax = ~n2_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2c, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_yminc, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfc_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfc_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(smooth_frame_n1a, file = "./plotly_objs/smooth_frame_n1a.rda")
save(smooth_frame_n2a, file = "./plotly_objs/smooth_frame_n2a.rda")
save(smooth_frame_n1b, file = "./plotly_objs/smooth_frame_n1b.rda")
save(smooth_frame_n2b, file = "./plotly_objs/smooth_frame_n2b.rda")
save(smooth_frame_n1c, file = "./plotly_objs/smooth_frame_n1c.rda")
save(smooth_frame_n2c, file = "./plotly_objs/smooth_frame_n2c.rda")
save(date_vec_n1a, file = "./plotly_objs/date_vec_n1a.rda")
save(date_vec_n2a, file = "./plotly_objs/date_vec_n2a.rda")
save(date_vec_n1b, file = "./plotly_objs/date_vec_n1b.rda")
save(date_vec_n2b, file = "./plotly_objs/date_vec_n2b.rda")
save(date_vec_n1c, file = "./plotly_objs/date_vec_n1c.rda")
save(date_vec_n2c, file = "./plotly_objs/date_vec_n2c.rda")
save(n1_ymina, file = "./plotly_objs/n1_ymina.rda")
save(n1_ymaxa, file = "./plotly_objs/n1_ymaxa.rda")
save(n2_ymina, file = "./plotly_objs/n2_ymina.rda")
save(n2_ymaxa, file = "./plotly_objs/n2_ymaxa.rda")

save(n1_yminb, file = "./plotly_objs/n1_yminb.rda")
save(n1_ymaxb, file = "./plotly_objs/n1_ymaxb.rda")
save(n2_yminb, file = "./plotly_objs/n2_yminb.rda")
save(n2_ymaxb, file = "./plotly_objs/n2_ymaxb.rda")

save(n1_yminc, file = "./plotly_objs/n1_yminc.rda")
save(n1_ymaxc, file = "./plotly_objs/n1_ymaxc.rda")
save(n2_yminc, file = "./plotly_objs/n2_yminc.rda")
save(n2_ymaxc, file = "./plotly_objs/n2_ymaxc.rda")